package com.imooc.animation360.view;

import com.imooc.animation360.R;
import com.imooc.animation360.engine.FloatViewManager;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;

public class FloatMenuView extends LinearLayout {
	//平移动画.
	private Animation translationAnimation;

	public FloatMenuView(Context context) {
		this(context,null);
	}

	public FloatMenuView(Context context, AttributeSet attrs) {
		this(context, attrs,0);
	}

	public FloatMenuView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		View root = View.inflate(getContext(), R.layout.float_menu_view, null);
		LinearLayout ll = (LinearLayout) root.findViewById(R.id.ll);
		
		//直观的效果就是从下往上弹出.
		translationAnimation = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 
				0, 
				Animation.RELATIVE_TO_SELF, 
				0, 
				Animation.RELATIVE_TO_SELF, 
				1.0f, 
				Animation.RELATIVE_TO_SELF, 
				0);
		translationAnimation.setDuration(500);
		translationAnimation.setFillAfter(true);
		ll.setAnimation(translationAnimation);
		
		
		//在float_menu_view.xml布局中, 给ll设置android:longClickable="true"或者android:clickable="true" 的目的是
		//可以拦截父容器root的onTouchListener事件,当点击ll所在区域时,不响应父容器的OnTouchListener,而是响应自己的onClickListener,
		//在root里非ll的区域则不会拦截.若不在xml里做手脚,在java类里ll.setOnClickListener(null)也是一样可以达到目的.
		root.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				FloatViewManager mFloatViewManager = FloatViewManager.getInstance(getContext());
				mFloatViewManager.hideFloatMenuView();
				mFloatViewManager.showFloatCircleView();
				return false;
			}
		});
		
		addView(root);
	}

	
	public void startAnimation(){
		translationAnimation.start();
	}
}
